package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import deprecated.hibernate.HibernateUtil;
import domain.dao.interfaces.IDAOContact;
import domain.entities.impl.User;

/**
 * Servlet implementation class AddContactServlet
 */
public class AddContactServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddContactServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ApplicationContext context = util.SpringUtils.getApplicationContext();

		String firstname = DomainUtils.trim(request.getParameter("first_name"));
		String lastname = DomainUtils.trim(request.getParameter("last_name"));
		String email = DomainUtils.trim(request.getParameter("e_mail"));
		String street = DomainUtils.trim(request.getParameter("street"));
		String city = DomainUtils.trim(request.getParameter("city"));
		String zip = DomainUtils.trim(request.getParameter("zip"));
		String country = DomainUtils.trim(request.getParameter("country"));
		String mobile = DomainUtils.trim(request.getParameter("mobile"));
		String house = DomainUtils.trim(request.getParameter("house"));
		String work = DomainUtils.trim(request.getParameter("work"));
		String siret = DomainUtils.trim(request.getParameter("num_siret"));
		String[] contactgroups = request.getParameterValues("contactgroups");


		try {

			checks(request, firstname, lastname, email, street, city, zip,
					country, mobile, house, work, siret, contactgroups);

			if (siret == null || siret.isEmpty()) {
				IDAOContact daoContact = (IDAOContact) context
						.getBean("beanDAOContact");

				daoContact.addContact( firstname, lastname, email, street,
						city, zip, country, mobile, house, work, contactgroups);
				// DAOFactory.getDaoContact().addContact(
				// null,
				// firstname, lastname, email, street, city, zip,
				// country, mobile, house, work, contactgroups);
			} else {
				int numSiret = new Integer(siret);
				IDAOContact daoContact = (IDAOContact) context
						.getBean("beanDAOContact");
				daoContact.addEntreprise( firstname, lastname, email,
						street, city, zip, country, mobile, house, work,
						numSiret, contactgroups);

				// DAOFactory.getDaoContact().addEntreprise(
				// null,
				// firstname, lastname, email, street, city, zip,
				// country, mobile, house, work, numSiret, contactgroups);
			}

			DomainUtils.displayInfoMessage(request,
					DomainUtils.ADD_CONTACT_ALERT_CURRENT_COLOR,
					"Contact successully added.");

		} catch (Exception e) {

			if ((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"An error occur on the server." + e);
			}

			DomainUtils.keepParametersContent(request);
		}

		request.getRequestDispatcher("pages/addContact.jsp").forward(request,
				response);
	}

	private void checks(HttpServletRequest request, String firstname,
			String lastname, String email, String street, String city,
			String zip, String country, String mobile, String house,
			String work, String siret, String[] contactgroups) throws Exception {

		HttpSession session = request.getSession(false);

		// Checking session
		if (session == null
				|| session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}

		// Checking firstname
		if (firstname == null
				|| firstname.isEmpty()
				|| DomainUtils.match(firstname, "[a-zA-Z]+(\\-[a-zA-Z]*)?") == false) {
			request.setAttribute(
					DomainUtils.ALERT_ERROR_STRING,
					"The field \"First Name\" should not be empty and should contain only letter or \"-\"");
			throw new FormArgumentsException();
		}

		// Checking lastname
		if (lastname == null
				|| lastname.isEmpty()
				|| DomainUtils.match(lastname,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {
			request.setAttribute(
					DomainUtils.ALERT_ERROR_STRING,
					"The field \"Last Name\" should not be empty and should contain only letter or space and \"-\" between words");
			throw new FormArgumentsException();
		}

		// Checking email

		// already checked in jsp
		if (email == null || email.isEmpty()) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"E-mail\" should not be empty");
			throw new FormArgumentsException();
		}

		// Checking street
		if (street == null || street.isEmpty()) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"Street\" should not be empty");
			throw new FormArgumentsException();
		}

		// Checking city
		if (city == null
				|| city.isEmpty()
				|| DomainUtils.match(city,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {
			request.setAttribute(
					DomainUtils.ALERT_ERROR_STRING,
					"The field \"City\" should not be empty and should contain only letter or space and \"-\" between words");
			throw new FormArgumentsException();
		}

		// Checking zip
		if (zip == null || zip.isEmpty() || zip.length() != 5
				|| DomainUtils.match(zip, "\\d*") == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"Zip code\" should not be empty and should contain only 5 digits ");
			throw new FormArgumentsException();
		}

		// Checking country
		if (country == null
				|| country.isEmpty()
				|| DomainUtils.match(country,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"Country\" should not be empty");
			throw new FormArgumentsException();
		}

		// Checking mobile
		if (!mobile.isEmpty()) {
			if (mobile == null || mobile.isEmpty() || mobile.length() != 10
					|| DomainUtils.match(mobile, "\\d*") == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"The field \"Mobile\" should not be empty and should contain only 10 digits");
				throw new FormArgumentsException();
			}
		}

		// Checking house
		if (!house.isEmpty()) {
			if (house == null || house.isEmpty() || house.length() != 10
					|| DomainUtils.match(house, "\\d*") == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"The field \"House\" should not be empty and should contain only 10 digits");
				throw new FormArgumentsException();
			}
		}

		// Checking work
		if (!work.isEmpty()) {
			if (work == null || work.isEmpty() || work.length() != 10
					|| DomainUtils.match(work, "\\d*") == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"The field \"Work\" should not be empty and should contain only 10 digits");
				throw new FormArgumentsException();
			}
		}

		// no need to check the contactgroups

		// Checking siret
		if (siret != null) {
			if (DomainUtils.match(siret, "\\d*") == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"The field \"Num Siret\" should  contain only digits");
				throw new FormArgumentsException();
			}
		}

	}

}
